﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
using nRoute.Behaviors.Interactivity;
using nRoute.Components.Messaging;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Animation;

namespace nRoute.Behaviors
{
    // based on Microsoft.Expression.Interactivity.Media.ControlStoryboardAction
    public class ControlStoryboardAction
        : TriggerAction<FrameworkElement>
    {
        public static readonly DependencyProperty StoryboardProperty =
            DependencyProperty.Register("Storyboard", typeof(Storyboard), typeof(ControlStoryboardAction),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty ControlStoryboardOptionProperty =
            DependencyProperty.Register("ControlStoryboardOption", typeof(ControlStoryboardOption), typeof(ControlStoryboardAction),
            new PropertyMetadata(ControlStoryboardOption.Pause));
        
#region Properties

        [Category("Common Properties")]
        public Storyboard Storyboard
        {
            get { return (Storyboard)GetValue(StoryboardProperty); }
            set { SetValue(StoryboardProperty, value); }
        }

        [Category("Common Properties")]
        public ControlStoryboardOption ControlStoryboardOption
        {
            get { return (ControlStoryboardOption)GetValue(ControlStoryboardOptionProperty); }
            set { SetValue(ControlStoryboardOptionProperty, value); }
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding StoryboardBinding
        {
            get { return GetBinding(StoryboardProperty); }
            set { SetBinding<Storyboard>(StoryboardProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ControlStoryboardOptionBinding
        {
            get { return GetBinding(ControlStoryboardOptionProperty); }
            set { SetBinding<ControlStoryboardOption>(ControlStoryboardOptionProperty, value); }
        }

#endif

#endregion

#region Trigger Related

        protected override void Invoke(object Type)
        {
            if (this.AssociatedObject == null || this.Storyboard == null) return;

            switch (this.ControlStoryboardOption)
            {
                case ControlStoryboardOption.Play:
                    this.Storyboard.Begin();
                    break;

                case ControlStoryboardOption.Stop:
                    this.Storyboard.Stop();
                    break;

                case ControlStoryboardOption.TogglePlayPause:
                    var _currentState = ClockState.Stopped;
                    try
                    {
                        _currentState = this.Storyboard.GetCurrentState();
                    }
                    catch (InvalidOperationException ex) { }

                    if (_currentState == ClockState.Stopped)
                    {
                        this.Storyboard.Resume();   // or Begin()
                        break;
                    }
                    else
                    {
                        this.Storyboard.Pause();
                        break;
                    }

                case ControlStoryboardOption.Pause:
                    this.Storyboard.Pause();
                    break;

                case ControlStoryboardOption.Resume:
                    this.Storyboard.Resume();
                    break;

                case ControlStoryboardOption.SkipToFill:
                    this.Storyboard.SkipToFill();
                    break;
            }
        }

#endregion

    }
}